package cn.com.lyj6851.modules.edu.service.impl;

import cn.com.lyj6851.common.service.impl.BaseServiceImpl;
import cn.com.lyj6851.common.utils.QueryHelpPlus;
import cn.com.lyj6851.dozer.service.IGenerator;
import cn.com.lyj6851.modules.edu.domain.EduSubject;
import cn.com.lyj6851.modules.edu.service.EduSubjectService;
import cn.com.lyj6851.modules.edu.service.dto.EduSubjectCriteria;
import cn.com.lyj6851.modules.edu.service.dto.EduSubjectDto;
import cn.com.lyj6851.modules.edu.service.mapper.EduSubjectMapper;
import cn.com.lyj6851.utils.FileUtil;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
@AllArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EduSubjectServiceImpl extends BaseServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
    private final IGenerator generator;


    @Override
    public Map<String, Object> queryAll(EduSubjectCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<EduSubject> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), EduSubjectDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<EduSubject> queryAll(EduSubjectCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(EduSubject.class,criteria));
    }

    @Override
    public void download(List<EduSubjectDto> all, HttpServletResponse response) throws IOException {
        List<Map<String,Object>> list = new ArrayList<>();
        for (EduSubjectDto eduSubject : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("分类名称",eduSubject.getTitle());
            map.put("创建时间",eduSubject.getCreateTime());
            map.put("创建者ID",eduSubject.getCreateId());
            list.add(map);
        }
        FileUtil.downloadExcel(list,response);
    }
}
